import requests
from datetime import date, timedelta
import datetime
import json
import re
import pymssql
import pymysql


def yesdkl():
    yesterday1 = (date.today() + timedelta(days=-1)).strftime("%Y%m%d")
    today = datetime.date.today()
    oneday = datetime.timedelta(days=1)
    yesterday = today - oneday  # 昨天日期
    reyesday = re.sub('-', '', str(yesterday))  # 定义数据库用的时间代码
    server = '47.103.69.43:9800\IPVA'  # 数据库地址
    user = 'sa'  # 数据库密码
    pw = 'ipva@07'  # 数据库密码
    Summaryday = "SELECT * FROM Summary_Day WHERE DateKey=" + reyesday + "AND SiteKey='P00021'"  # 定义查询语句变量
    conn = pymssql.connect(server, user, pw, database="IPVA_S0400_B1")  # 连接数据库
    cur = conn.cursor()
    cur.execute(Summaryday)  # 执行查询语句
    klrow = cur.fetchone()  # 返回数据
    dayklinsum = str(klrow[6])  # 存放昨日进客流数据
    daykloutsum = str(klrow[7])  # 存放昨日出客流
    return dayklinsum, daykloutsum


def yesdcar():
    yesterday1 = (date.today() + timedelta(days=-1)).strftime("%Y%m%d")
    today = datetime.date.today()
    oneday = datetime.timedelta(days=1)
    yesterday = today - oneday  # 昨天日期
    reyesday = re.sub('-', '', str(yesterday))  # 定义数据库用的时间代码
    # 昨日车流数据start
    parkheader = {
        'Host': '47.103.69.43:7333',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36',
    }
    url = 'http://47.103.69.43:7333/park/home/stat/flow.htm?1587526475362&startTime=' + str(
        yesterday) + '%2000%3A00%3A00&endTime=' + str(
        yesterday) + '%2023%3A59%3A59&averageFlag=0&statType=1&loginOperatorName=wangrui&pwd=df6ebbd720969fa1bd08a86b220ae989'
    pkreq = requests.get(url, headers=parkheader)
    pkreqjs = pkreq.content.decode()
    pkreqjson = json.loads(pkreqjs)
    parkin = str(pkreqjson['data']['list'][0]['totalFlowIn'])
    parkout = str(pkreqjson['data']['list'][0]['totalFlowOut'])
    return parkin
